(require 'cl)

;--------- Some time-relate function ------------------------
(defun insert-current-time ()
  (interactive)
  (insert (format-time-string "%H:%M:%S" (current-time))))
(define-key global-map "\C-ct" 'insert-current-time)
(defun insert-current-day ()
  (interactive)
  (insert (format-time-string "%y-%m-%d-%A" (current-time))))
(defun insert-log-current-time ()
  (interactive)
  (let ((delaymin 30)
      a
      b)
  (setq a (format-time-string "%H:%M" (current-time)))
  (setq b (format-time-string "%H:%M"
			      (time-add (current-time) (seconds-to-time (* delaymin 60)))))
  (insert (concat "[" a "--" b "] "))))
(defun insert-current-week ()
  (interactive)
  (insert (format-time-string "%Y-Week:%U" (current-time)))) 
(define-key global-map "\C-cy" 'insert-current-day)
(define-key global-map "\C-cg" 'insert-log-current-time)
(define-key global-map "\C-cw" 'widen)

;----- Some global setting -----------------------------
(global-set-key (kbd "C-\\") 'hippie-expand)
(global-set-key (kbd "C-;") 'ido-switch-buffer)
(global-set-key (kbd "C-'") 'other-window)
;; (global-set-key (kbd "C-`") 'speedbar-get-focus)
;; (global-set-key (kbd "M-;") 'tmm-menubar) 
(global-unset-key (kbd "<mouse-2>") )
(global-set-key (kbd "<mouse-2>") 'execute-extended-command)
(global-set-key (kbd "C-c C-s") 'shell)
(global-set-key (kbd "<C-f3>") 'kmacro-end-or-call-macro)
(global-set-key (kbd "<f3>") 'kmacro-start-macro-or-insert-counter)
(global-set-key (kbd "<M-f3>") 'kmacro-name-last-macro)
(global-set-key (kbd "\C-c\C-r") 'windresize)
(global-set-key (kbd "<escape>") 'emaci-mode)
(global-unset-key (kbd "M-`") )
(global-unset-key (kbd "C-l"))
(global-unset-key (kbd "<C-f9>"))
(global-unset-key (kbd "<f9>"))
(global-unset-key (kbd "<f10>"))
(global-set-key (kbd "C-l") 'goto-line)
(global-set-key (kbd "<f4>") 'bm-toggle)
(global-set-key (kbd "<C-f4>") 'bm-previous)
(global-set-key (kbd "<S-f4>") 'bm-next)
(global-set-key (kbd "<f5>") 'ispell-word)
(global-set-key (kbd "<f1>") 'emaci-mode)
(global-set-key (kbd "C-q") 'emaci-mode)
(global-set-key (kbd "<C-f5>") 'ac-expand-ispell-word)
(global-set-key (kbd "<M-f5>") 'sdcv-search-pointer+)
(global-set-key (kbd "<f8>") 'google-translate-en-zh)
(global-set-key (kbd "<M-f8>") 'google-translator-query-translate)
(define-key global-map (kbd "<C-f8>") 'google-translate-query-en-zh)
(global-set-key (kbd "<M-f9>") 'org-capture)
;; (global-unset-key (kbd "<f4>"))
;; (global-set-key (kbd "<f10>") 'org-clock-in)
(global-set-key (kbd "<f10>") 'xwp/org-clock-default-tasks)
(global-set-key (kbd "<C-f10>") 'org-clock-out)
(global-set-key (kbd "<M-f10>") 'org-clock-goto)
(global-set-key (kbd "<f9>") 'org-agenda)
(setq bm-cycle-all-buffers t)

;---- Paren match function ----------------------------
(defun goto-paren ()
"跳到匹配的括号"
(interactive)
(cond
((looking-at "[ \t]*[[\"({]") (forward-sexp) (backward-char))
((or (looking-at "[]\")}]") (looking-back "[]\")}][ \t]*")) (if (< (point) (point-max)) (forward-char)) (backward-sexp))
(t (message "找不到匹配的括号"))))

;; Define comment function
(defun qiang-comment-dwim-line (&optional arg)
  "Replacement for the comment-dwim command.
If no region is selected and current line is not blank and we are not at the end of the line,
then comment current line.
Replaces default behaviour of comment-dwim, when it inserts comment at the end of the line."
  (interactive "*P")
  (comment-normalize-vars)
  (if (and (not (region-active-p)) (not (looking-at "[ \t]*$")))
      (comment-or-uncomment-region (line-beginning-position) (line-end-position))
    (comment-dwim arg)))		
(global-set-key "\M-;" 'qiang-comment-dwim-line)

;; set bookmarks
(defadvice pop-to-buffer (before cancel-other-window first)
  (ad-set-arg 1 nil))
(ad-activate 'pop-to-buffer)
;; Toggle window dedication
(defun toggle-window-dedicated ()
  "Toggle whether the current active window is dedicated or not"
  (interactive)
  (message
   (if (let (window (get-buffer-window (current-buffer)))
         (set-window-dedicated-p window 
                                 (not (window-dedicated-p window))))
       "Window '%s' is dedicated"
     "Window '%s' is normal")
   (current-buffer)))
(global-set-key [pause] 'toggle-window-dedicated)

(defun my-maximized ()  
  (interactive)  
  (x-send-client-message  
    nil 0 nil "_NET_WM_STATE" 32  
    '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0)  
  )  
  (x-send-client-message  
    nil 0 nil "_NET_WM_STATE" 32  
    '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0)  
  )  
)
;; 切换输入法函数
(defun fcitx-set-status ()
  (interactive)
  (call-process "fcitx-remote" nil nil nil "-t"))

(windmove-default-keybindings 'super)
(provide 'mycmd)
